"""Smoke simulation using alpha-blended point particles"""

__version__ = '$Id: smoke.py 86 2008-07-10 04:49:05Z casey.duncan $'

from pyglet import image
from pyglet.gl import *
import sys, os
sys.path.append('../..')

from particle import Particle, ParticleGroup, default_system
from particle.renderer import PointRenderer, TexturedRenderer, FireRenderer
from particle.emitter import StaticEmitter
from particle.controller import Gravity, Lifetime, Collector, Movement, Fader
from particle.domain import Box

if __name__ == '__main__':
	"""This file is meant to show how to use controller, renderer, system, particle, group to implement
	the effect in pyparticle.py"""
	win = pyglet.window.Window(resizable=True, visible=False)


	def resize(widthWindow, heightWindow):
		"""Initial settings for the OpenGL state machine, clear color, window size, etc"""
		glEnable(GL_LIGHTING)
		glEnable(GL_LIGHT0)
		glShadeModel(GL_SMOOTH)
		glEnable(GL_COLOR_MATERIAL)
		glMatrixMode(GL_PROJECTION)
		glLoadIdentity()
		glMatrixMode(GL_MODELVIEW)
		glEnable(GL_TEXTURE_2D)
		glDisable(GL_DEPTH_TEST)
		glEnable(GL_BLEND)
		glBlendFunc(GL_SRC_ALPHA,GL_ONE)
	#Dimensions of flame boundry box
	x1      = 300
	y1      = 25
	z1      = 0
	x2  	= 400
	y2 	= 100
	width  = 1.5
	height = 1.5
	dept = 0.0
	fire = StaticEmitter(
		rate=350,
		template=Particle(
			position=(x1,y1,z1),
			size =(width, height, dept),
			velocity=(0,25,0), 
			color=(1.0, 0.2, 0.0, 1.0),
			life = 1.0,
		),
		deviation=Particle(position=(2,15,0), velocity=(2.5,5,0),  size=(8.5,8.5,0.0))
	)
	default_system.add_global_controller(
		Lifetime(20),
		Gravity((0, -1, 0)), 
		Movement(),
		Collector(Box((0,0,0), (x2,y2,0)), collect_inside=False)
		)
	group1 = ParticleGroup(controllers=[fire], renderer=FireRenderer(
		os.path.join(os.path.dirname(__file__), 'particle_fire.bmp'), 3.5))

	win.resize = resize
	win.set_visible(True)
	win.resize(win.width, win.height)
	pyglet.clock.schedule_interval(default_system.update, (1.0/30.0))

	@win.event
	def on_draw():
		win.clear()
		glLoadIdentity()
		default_system.draw()

	pyglet.app.run()
